package com.omazan.db;

import java.util.List;

import org.hibernate.Session;

import com.omazan.db.models.Administrator;
import com.omazan.db.models.Customer;
import com.omazan.db.models.Employee;
import com.omazan.db.models.User;
import com.omazan.db.models.UserType;
import com.omazan.utils.Constants;
import com.omazan.utils.OmazanException;

public class InitDemoData {

	public static void main(String[] args) throws OmazanException {

		// User type 1.
		UserType userType = new UserType();

		userType.setUserType(Constants.admin);
		userType.setActive(true);

		Session session = GeneralDBMethods.getInstance().getSessionFactoryForDbOperation().openSession();
		session.beginTransaction();

		session.save(userType);
		session.getTransaction().commit();
		session.close();

		// User type 2.
		userType = new UserType();

		userType.setUserType(Constants.emp);
		userType.setActive(true);

		session = GeneralDBMethods.getInstance().getSessionFactoryForDbOperation().openSession();
		session.beginTransaction();

		session.save(userType);
		session.getTransaction().commit();
		session.close();

		// User type 3.
		userType = new UserType();

		userType.setUserType(Constants.cust);
		userType.setActive(true);

		session = GeneralDBMethods.getInstance().getSessionFactoryForDbOperation().openSession();
		session.beginTransaction();

		session.save(userType);
		session.getTransaction().commit();
		session.close();

		// USer Admin.
		User user = new User();
		user.setPassword("linux123");
		user.setActive(true);
		user.setUserName("admin");
		
		user.getUserType().setUserTypeId(1);

		Administrator administrator = new Administrator();
		administrator.setActive(true);
		administrator.setCity("Darmstadt");
		administrator.setCountry("Germany");
		administrator.setEmail("admin@omazan.com");
		administrator.setFirstName("super");
		administrator.setHouseNo("3");
		administrator.setLastName("user");
		administrator.setMobileNo("123456789");
		administrator.setState("hessen");
		administrator.setUser(user);
		
		session = GeneralDBMethods.getInstance().getSessionFactoryForDbOperation().openSession();
		session.beginTransaction();

		session.save(user);
		session.save(administrator);
		session.getTransaction().commit();
		session.close();

		// USer Employee.
		user = new User();
		user.setPassword("linux123");
		user.setActive(true);
		user.setUserName("emp");
		
		user.getUserType().setUserTypeId(2);
		
		Employee employee = new Employee();
		employee.setActive(true);
		employee.setCity("Darmstadt");
		employee.setCountry("Germany");
		employee.setEmail("john_emp@omazan.com");
		employee.setFirstName("john");
		employee.setHouseNo("3");
		employee.setLastName("Emp");
		employee.setMobileNo("123456789");
		employee.setState("hessen");
		employee.setUser(user);

		session = GeneralDBMethods.getInstance().getSessionFactoryForDbOperation().openSession();
		session.beginTransaction();

		session.save(user);
		session.save(employee);
		session.getTransaction().commit();
		session.close();

		// USer Customer.
		user = new User();
		user.setPassword("linux123");
		user.setActive(true);
		user.setUserName("cust");
		user.getUserType().setUserTypeId(3);

		Customer customer = new Customer();
		customer.setActive(true);
		customer.setCity("Darmstadt");
		customer.setCountry("Germany");
		customer.setEmail("lenny_cust@omazan.com");
		customer.setFirstName("Lenny");
		customer.setHouseNo("3");
		customer.setLastName("Cust");
		customer.setMobileNo("123456789");
		customer.setState("hessen");
		customer.setUser(user);
		
		session = GeneralDBMethods.getInstance().getSessionFactoryForDbOperation().openSession();
		session.beginTransaction();

		session.save(user);
		session.save(customer);
		session.getTransaction().commit();
		session.close();
		
		List<User> allUsers = new HandleUsers().readAllUsersFromDB();
		for(User user2 : allUsers){
			
			System.out.println(user2.getUserName());
		}
		
		

	}

}
